package com.dianping.cat.configuration.server.transform;

import com.dianping.cat.configuration.server.IEntity;
import com.dianping.cat.configuration.server.entity.ConsumerConfig;
import com.dianping.cat.configuration.server.entity.Domain;
import com.dianping.cat.configuration.server.entity.HarfsConfig;
import com.dianping.cat.configuration.server.entity.HdfsConfig;
import com.dianping.cat.configuration.server.entity.LongConfig;
import com.dianping.cat.configuration.server.entity.Property;
import com.dianping.cat.configuration.server.entity.Server;
import com.dianping.cat.configuration.server.entity.ServerConfig;
import com.dianping.cat.configuration.server.entity.StorageConfig;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;














public class DefaultSaxParser
  extends DefaultHandler
{
/*  43 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  45 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  47 */   private Stack<String> m_tags = new Stack();

/*  49 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  53 */   private StringBuilder m_text = new StringBuilder();


/*  56 */   public static ServerConfig parse(InputStream in) throws SAXException, IOException { return (ServerConfig)parseEntity(ServerConfig.class, new InputSource((InputStream)removeBOM(in))); }



/*  60 */   public static ServerConfig parse(Reader reader) throws SAXException, IOException { return (ServerConfig)parseEntity(ServerConfig.class, new InputSource((Reader)removeBOM(reader))); }



/*  64 */   public static ServerConfig parse(String xml) throws SAXException, IOException { return (ServerConfig)parseEntity(ServerConfig.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  70 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  71 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  73 */       factory.setValidating(false);
/*  74 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  76 */       factory.newSAXParser().parse(is, handler);
/*  77 */       return (T)handler.getEntity();
    } catch (ParserConfigurationException e) {
/*  79 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  84 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  88 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  94 */     if (obj instanceof String) {
/*  95 */       String str = (String)obj;

/*  97 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  98 */         return (T)str.substring(1);
      }
/* 100 */       return obj;
    }
/* 102 */     if (obj instanceof InputStream) {
/* 103 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 105 */       in.mark(3);

/* 107 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 108 */         in.reset();
      }

/* 111 */       return (T)in;
/* 112 */     }  if (obj instanceof Reader) {
/* 113 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 115 */       in.mark(1);

/* 117 */       if (in.read() != 65279) {
/* 118 */         in.reset();
      }

/* 121 */       return (T)in;
    }
/* 123 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 129 */     if (value == null || value.length() == 0) {
/* 130 */       return defaultValue;
    }

/* 133 */     if (type == Boolean.class)
/* 134 */       return (T)Boolean.valueOf(value); 
/* 135 */     if (type == Integer.class)
/* 136 */       return (T)Integer.valueOf(value); 
/* 137 */     if (type == Long.class)
/* 138 */       return (T)Long.valueOf(value); 
/* 139 */     if (type == Short.class)
/* 140 */       return (T)Short.valueOf(value); 
/* 141 */     if (type == Float.class)
/* 142 */       return (T)Float.valueOf(value); 
/* 143 */     if (type == Double.class)
/* 144 */       return (T)Double.valueOf(value); 
/* 145 */     if (type == Byte.class)
/* 146 */       return (T)Byte.valueOf(value); 
/* 147 */     if (type == Character.class) {
/* 148 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 150 */     return (T)value;
  }




/* 156 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 161 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 166 */     if (uri == null || uri.length() == 0) {
/* 167 */       this.m_objs.pop();
/* 168 */       this.m_tags.pop();
    }


/* 172 */     this.m_text.setLength(0);
  }


/* 176 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 180 */   protected String getText() { return this.m_text.toString(); }


  private void parseForConsumer(ConsumerConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 184 */     if ("long-config".equals(qName)) {
/* 185 */       LongConfig longConfig = this.m_maker.buildLongConfig(attributes);

/* 187 */       this.m_linker.onLongConfig(parentObj, longConfig);
/* 188 */       this.m_objs.push(longConfig);
    } else {
/* 190 */       throw new SAXException(String.format("Element(%s) is not expected under consumer!", new Object[] { qName }));
    }

/* 193 */     this.m_tags.push(qName);
  }

  private void parseForDomain(Domain parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 197 */     this.m_objs.push(parentObj);
/* 198 */     this.m_tags.push(qName);
  }

  private void parseForHarfs(HarfsConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 202 */     this.m_objs.push(parentObj);
/* 203 */     this.m_tags.push(qName);
  }

  private void parseForHdfs(HdfsConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 207 */     this.m_objs.push(parentObj);
/* 208 */     this.m_tags.push(qName);
  }

  private void parseForLongConfig(LongConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 212 */     if ("domain".equals(qName)) {
/* 213 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 215 */       this.m_linker.onDomain(parentObj, domain);
/* 216 */       this.m_objs.push(domain);
    } else {
/* 218 */       throw new SAXException(String.format("Element(%s) is not expected under long-config!", new Object[] { qName }));
    }

/* 221 */     this.m_tags.push(qName);
  }

  private void parseForProperty(Property parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 225 */     this.m_objs.push(parentObj);
/* 226 */     this.m_tags.push(qName);
  }

  private void parseForServer(Server parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 230 */     if ("properties".equals(qName)) {
/* 231 */       this.m_objs.push(parentObj);
/* 232 */     } else if ("property".equals(qName)) {
/* 233 */       Property property = this.m_maker.buildProperty(attributes);

/* 235 */       this.m_linker.onProperty(parentObj, property);
/* 236 */       this.m_objs.push(property);
/* 237 */     } else if ("storage".equals(qName)) {
/* 238 */       StorageConfig storage = this.m_maker.buildStorage(attributes);

/* 240 */       this.m_linker.onStorage(parentObj, storage);
/* 241 */       this.m_objs.push(storage);
/* 242 */     } else if ("consumer".equals(qName)) {
/* 243 */       ConsumerConfig consumer = this.m_maker.buildConsumer(attributes);

/* 245 */       this.m_linker.onConsumer(parentObj, consumer);
/* 246 */       this.m_objs.push(consumer);
    } else {
/* 248 */       throw new SAXException(String.format("Element(%s) is not expected under server!", new Object[] { qName }));
    }

/* 251 */     this.m_tags.push(qName);
  }

  private void parseForServerConfig(ServerConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 255 */     if ("server".equals(qName)) {
/* 256 */       Server server = this.m_maker.buildServer(attributes);

/* 258 */       this.m_linker.onServer(parentObj, server);
/* 259 */       this.m_objs.push(server);
    } else {
/* 261 */       throw new SAXException(String.format("Element(%s) is not expected under server-config!", new Object[] { qName }));
    }

/* 264 */     this.m_tags.push(qName);
  }

  private void parseForStorage(StorageConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 268 */     if ("properties".equals(qName)) {
/* 269 */       this.m_objs.push(parentObj);
/* 270 */     } else if ("hdfs".equals(qName)) {
/* 271 */       HdfsConfig hdfs = this.m_maker.buildHdfs(attributes);

/* 273 */       this.m_linker.onHdfs(parentObj, hdfs);
/* 274 */       this.m_objs.push(hdfs);
/* 275 */     } else if ("harfs".equals(qName)) {
/* 276 */       HarfsConfig harfs = this.m_maker.buildHarfs(attributes);

/* 278 */       this.m_linker.onHarfs(parentObj, harfs);
/* 279 */       this.m_objs.push(harfs);
/* 280 */     } else if ("property".equals(qName)) {
/* 281 */       Property property = this.m_maker.buildProperty(attributes);

/* 283 */       this.m_linker.onProperty(parentObj, property);
/* 284 */       this.m_objs.push(property);
    } else {
/* 286 */       throw new SAXException(String.format("Element(%s) is not expected under storage!", new Object[] { qName }));
    }

/* 289 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 293 */     if ("server-config".equals(qName)) {
/* 294 */       ServerConfig serverConfig = this.m_maker.buildServerConfig(attributes);

/* 296 */       this.m_entity = serverConfig;
/* 297 */       this.m_objs.push(serverConfig);
/* 298 */       this.m_tags.push(qName);
/* 299 */     } else if ("server".equals(qName)) {
/* 300 */       Server server = this.m_maker.buildServer(attributes);

/* 302 */       this.m_entity = server;
/* 303 */       this.m_objs.push(server);
/* 304 */       this.m_tags.push(qName);
/* 305 */     } else if ("property".equals(qName)) {
/* 306 */       Property property = this.m_maker.buildProperty(attributes);

/* 308 */       this.m_entity = property;
/* 309 */       this.m_objs.push(property);
/* 310 */       this.m_tags.push(qName);
/* 311 */     } else if ("storage".equals(qName)) {
/* 312 */       StorageConfig storage = this.m_maker.buildStorage(attributes);

/* 314 */       this.m_entity = storage;
/* 315 */       this.m_objs.push(storage);
/* 316 */       this.m_tags.push(qName);
/* 317 */     } else if ("hdfs".equals(qName)) {
/* 318 */       HdfsConfig hdfs = this.m_maker.buildHdfs(attributes);

/* 320 */       this.m_entity = hdfs;
/* 321 */       this.m_objs.push(hdfs);
/* 322 */       this.m_tags.push(qName);
/* 323 */     } else if ("harfs".equals(qName)) {
/* 324 */       HarfsConfig harfs = this.m_maker.buildHarfs(attributes);

/* 326 */       this.m_entity = harfs;
/* 327 */       this.m_objs.push(harfs);
/* 328 */       this.m_tags.push(qName);
/* 329 */     } else if ("consumer".equals(qName)) {
/* 330 */       ConsumerConfig consumer = this.m_maker.buildConsumer(attributes);

/* 332 */       this.m_entity = consumer;
/* 333 */       this.m_objs.push(consumer);
/* 334 */       this.m_tags.push(qName);
/* 335 */     } else if ("long-config".equals(qName)) {
/* 336 */       LongConfig longConfig = this.m_maker.buildLongConfig(attributes);

/* 338 */       this.m_entity = longConfig;
/* 339 */       this.m_objs.push(longConfig);
/* 340 */       this.m_tags.push(qName);
/* 341 */     } else if ("domain".equals(qName)) {
/* 342 */       Domain domain = this.m_maker.buildDomain(attributes);

/* 344 */       this.m_entity = domain;
/* 345 */       this.m_objs.push(domain);
/* 346 */       this.m_tags.push(qName);
    } else {
/* 348 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 354 */     if (uri == null || uri.length() == 0) {
/* 355 */       if (this.m_objs.isEmpty()) {
/* 356 */         parseRoot(qName, attributes);
      } else {
/* 358 */         Object parent = this.m_objs.peek();
/* 359 */         String tag = (String)this.m_tags.peek();

/* 361 */         if (parent instanceof ServerConfig) {
/* 362 */           parseForServerConfig((ServerConfig)parent, tag, qName, attributes);
/* 363 */         } else if (parent instanceof Server) {
/* 364 */           parseForServer((Server)parent, tag, qName, attributes);
/* 365 */         } else if (parent instanceof Property) {
/* 366 */           parseForProperty((Property)parent, tag, qName, attributes);
/* 367 */         } else if (parent instanceof StorageConfig) {
/* 368 */           parseForStorage((StorageConfig)parent, tag, qName, attributes);
/* 369 */         } else if (parent instanceof HdfsConfig) {
/* 370 */           parseForHdfs((HdfsConfig)parent, tag, qName, attributes);
/* 371 */         } else if (parent instanceof HarfsConfig) {
/* 372 */           parseForHarfs((HarfsConfig)parent, tag, qName, attributes);
/* 373 */         } else if (parent instanceof ConsumerConfig) {
/* 374 */           parseForConsumer((ConsumerConfig)parent, tag, qName, attributes);
/* 375 */         } else if (parent instanceof LongConfig) {
/* 376 */           parseForLongConfig((LongConfig)parent, tag, qName, attributes);
/* 377 */         } else if (parent instanceof Domain) {
/* 378 */           parseForDomain((Domain)parent, tag, qName, attributes);
        } else {
/* 380 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 384 */       this.m_text.setLength(0);
    } else {
/* 386 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/server/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */